#include "C_Game.h"
#include "C_GS_MainMenu.h"
#include "C_GS_Intro.h"
#include "C_GS_Game.h"
#include "C_GS_ShipEditor.h"
#include "C_GS_FusionEditor.h"

/////////////////////////////////////////////////////////////////////////////
//	CONSTRUCTOR
/////////////////////////////////////////////////////////////////////////////
C_Game::C_Game()
: m_width(900)
, m_height(600)
{
	sf::VideoMode display = sf::VideoMode::getDesktopMode();

	SC_Size::setWidth(/*display.width*/1024);
	SC_Size::setHeight(/*display.height*/600);

	m_width = WINDOW_WIDTH;
	m_height = WINDOW_HEIGHT;

	m_window = new sf::RenderWindow(sf::VideoMode(m_width, m_height), "InfiniteSpace"/*, sf::Style::Fullscreen*/);
	m_window->setMouseCursorVisible(false);
	m_window->setVerticalSyncEnabled(true);

	LOAD_FONT_1("data/fonts/unifont7.ttf");

	VC_Drawable::setRenderWindow(m_window);
	C_Texture::setTextureManager(&m_TextureMngr);
	VC_GameState::setFSM(&m_fsm);

	m_cursor.loadFromFile("data/textures/cursor1.png");
	m_cursor.setSize(32.0f, 32.0f);
	m_fsm.push(new C_GS_FusionEditor());

	initGL();

}

/////////////////////////////////////////////////////////////////////////////
//	DESTRUCTOR
/////////////////////////////////////////////////////////////////////////////
C_Game::~C_Game()
{
	delete m_window;
}


/////////////////////////////////////////////////////////////////////////////
//	EXECUTION
/////////////////////////////////////////////////////////////////////////////
int C_Game::exec()
{
	const sf::Time timePerFrame = sf::seconds(1.f / 60.f);
	sf::Clock clock;
	sf::Time timeSinceLastUpdate = sf::Time::Zero;

	while (m_window->isOpen())
	{
		handleEvent();
		timeSinceLastUpdate += clock.restart();
		while (timeSinceLastUpdate > timePerFrame)
		{
			timeSinceLastUpdate -= timePerFrame;
			handleEvent();
			update();
		}
		render();
	}

	return 0;
}


/////////////////////////////////////////////////////////////////////////////
//	HANDLE EVENT
/////////////////////////////////////////////////////////////////////////////
void C_Game::handleEvent()
{
	sf::Event event;

	while (m_window->pollEvent(event))
	{
		switch (event.type)
		{
		case sf::Event::Closed:
		{
								  m_window->close();
								  break;
		}
		case sf::Event::KeyPressed:
		{
									  if (event.key.code == sf::Keyboard::Num1)
									  {
										  m_fsm.change(new C_GS_Intro());
									  }
									  if (event.key.code == sf::Keyboard::Num2)
									  {
										  m_fsm.change(new C_GS_MainMenu());
									  }
									  if (event.key.code == sf::Keyboard::Escape)
									  {
										  m_window->close();
									  }
									  break;
		}
		case sf::Event::MouseMoved:
		{
									  m_cursor.setPos(event.mouseMove.x, event.mouseMove.y);
									  break;
		}
		default:
			break;
		}
		m_fsm.handleEvent(event);
	}
}


/////////////////////////////////////////////////////////////////////////////
//	UPDATE
/////////////////////////////////////////////////////////////////////////////
void C_Game::update()
{
	m_fsm.update();
}


/////////////////////////////////////////////////////////////////////////////
//	RENDER
/////////////////////////////////////////////////////////////////////////////
void C_Game::render()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	m_fsm.render();

	glPointSize(4.0f);
	glBegin(GL_POINTS);
	glColor3f(1.0f, 1.0f, 0.0f);
	glVertex2f(WINDOW_WIDTH / 2.0f, WINDOW_HEIGHT / 2.0f);
	glEnd();

	m_cursor.render();
	m_window->display();
}


/////////////////////////////////////////////////////////////////////////////
//	IITIALIZATION OPENGL
/////////////////////////////////////////////////////////////////////////////
void C_Game::initGL()
{
	glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

	glViewport(0, 0, (GLsizei)m_width, (GLsizei)m_height);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(0.0f, m_width, m_height, 0.0f, 1, -1);

	glMatrixMode(GL_MODELVIEW);
}
